<?php
class ZFenxiao {
	
	static public function OpenTuanSuccessed($pin_id)
	{
		$open_tuan_user_arr = get_pintuan_tuanzhang($pin_id);
		$tuanzhagn_info = $open_tuan_user_arr['tuanzhang'];
		
		if($tuanzhagn_info['flag'] == 0)
		{
			self::ToBeFener($tuanzhagn_info['id']);
		}
		ZPoint::PinTuanBuy($pin_id);
		
		$pin_order_list = DB::LimitQuery('pin_order_list',array(
			'condition' => array('pin_id'=>$pin_id),
			'order' => 'ORDER BY createtime ASC',
		));
		
		foreach($pin_order_list as $o_list)
		{
			$val = Table::Fetch('order', $o_list['order_id']);
			if(!empty($val) &&  $val['state'] == 'pay')
			{
				
				$tmp_user = Table::Fetch('user', $val['user_id']);	
				if($tmp_user['id'] == $tuanzhagn_info['id'])
				{
					continue;
				} else if( $tmp_user['shareid'] == 0)
				{
					self::SaveShareid($tmp_user['id'],$tuanzhagn_info['id']);
					$val['commission_id'] = $tuanzhagn_info['id'];
					Table::UpdateCache('order', $val['id'], array(
						'commission_id' => $tuanzhagn_info['id'],
					));
				}
				self::SendCommismoney($val);
			}
		}
	}
	
	static public function GetUserTuanGroupCount($user_id)
	{
		$condition = array(
			'shareid' => $user_id,
		);

		$count = Table::Count('user', $condition);
		return $count;
	}
	
	static public function GetTuanOrderCount($user_id)
	{
		$condition = array(
			'commission_id' => $user_id,
			'rstate'=>'normal'
		);

		$count = Table::Count('order', $condition);
		return $count;
	}
	
	//share_history
	static public function SendCommismoney($order)
	{
		//user commismoney
		//order  commission_id  commission_money
		if($order['commission_id'] >0 && $order['commission_money'] > 0)
		{ 
			Table::UpdateCache('user', $order['commission_id'], array(
				'commismoney' => array( "`commismoney`+{$order[commission_money]}" ),
			));
			notify_addorder_commismoney($order);
		}
	}
	static public function AddFxRelation($rmid,$user_id)
	{
		ZPoint::Invite($rmid,$user_id);
	}
	
	static public function SaveShareid($user_id ,$add_user_id)
	{
		Table::UpdateCache('user', $user_id, array(
				'shareid' => $add_user_id,
			));
		notify_addnew_user($user_id ,$add_user_id);
	}
	
	static public function ToBeFener($user_id){
		Table::UpdateCache('user', $user_id, array(
				'flag' => 1,
			));
	}
	
	static public function CashIt($order) {
		global $login_user_id;
		if (! $order['state']=='pay' ) return 0;

		//update order
		Table::UpdateCache('order', $order['id'], array(
					'state' => 'pay',
					'service' => 'cash',
					'admin_id' => $login_user_id,
					'money' => $order['origin'],
					'pay_time' => time(),
					));
		  /* cash flow */
		$order = Table::FetchForce('order', $order['id']);
                ZFlow::CreateFromStore($order['user_id'], $order['origin']);
		ZTeam::BuyOne($order);
	}

	static public function CreateFromCharge($money, $user_id, $time,$service) {
		if (!$money || !$user_id || !$time || !$service) return 0;
		$pay_id = "charge-{$user_id}-{$time}";
		$oarray = array(
				'user_id' => $user_id,
				'pay_id' => $pay_id,
				'service' => $service,
				'state' => 'pay',
				'money' => $money,
				'origin' => $money,
				'create_time' => $time,
			       );
		return DB::Insert('order', $oarray);
	}
}
?>
